from app.Pojo.BOrder import BOrder
from app.db import db


class OrderMapper:
    def __init__(self, db):
        self.db = db
    def list_orders(self, page, page_size):
        """分页查询订单"""
        query = BOrder.query
        total = query.count()
        orders = query.order_by(BOrder.order_time.desc()) \
            .limit(page_size) \
            .offset((page - 1) * page_size) \
            .all()
        return orders, total

    def batch_delete(self, ids):
        """批量删除订单"""
        BOrder.query.filter(BOrder.id.in_(ids)).delete(synchronize_session=False)
        db.session.commit()

    def delete(self, order_id):
        """删除订单"""
        BOrder.query.filter(BOrder.id == order_id).delete(synchronize_session=False)
        db.session.commit()